package bg.tu.varna.pms.client.listener;

import java.util.ArrayList;
import java.util.List;

import org.omg.CORBA.portable.ApplicationException;

import bg.tu.varna.pms.client.controller.MessagingController;
import bg.tu.varna.pms.model.ChatMessage;
import bg.tu.varna.pms.model.ChatRoom;
import bg.tu.varna.pms.model.Contact;
import bg.tu.varna.pms.model.FileMessage;
import bg.tu.varna.pms.model.communication.FriendshipMessage.Type;
import bg.tu.varna.pms.model.communication.SearchMessage.Response;
import bg.tu.varna.pms.model.communication.SyncMessage.OperationType;

public class UserEventImpl implements UserEvent {

//	fill those!;
	/** The user group controllers. */
	private final List<UserGroupListener> userGroupListeners = new ArrayList<UserGroupListener>();
	
	/** The contactlist controllers. */
	private final List<ContactlistListener> contactlistListeners = new ArrayList<ContactlistListener>();
	
	/** The messaging controllers. */
	private final List<MessagingListener> messagingListeners = new ArrayList<MessagingListener>();

	// Call it from facade which has instances of all listeners
	private static final UserEventImpl instance = new UserEventImpl();
	
	public static final UserEventImpl getInstance() {
		return instance;
	}
	
	/**
	 * Adds the user group controller.
	 *
	 * @param controller the controller
	 */
	public void addUserGroupListener(final UserGroupListener listener) {
		userGroupListeners.add(listener);
	}
	
	/**
	 * Removes the user group controller.
	 *
	 * @param controller the controller
	 * @return true, if successful
	 */
	public boolean removeUserGroupListener(final UserGroupListener listener) {
		return userGroupListeners.remove(listener);
	}
	
	/**
	 * Adds the contactlist controller.
	 *
	 * @param controller the controller
	 */
	public void addContactlistListener(final ContactlistListener listener) {
		contactlistListeners.add(listener);
	}
	
	/**
	 * Removes the contractlist controller.
	 *
	 * @param controller the controller
	 * @return true, if successful
	 */
	public boolean removeContractlistListener(final ContactlistListener listener) {
		return contactlistListeners.remove(listener);
	}
	
	/**
	 * Adds the messaging controllers.
	 *
	 * @param conroller the conroller
	 */
	public void addMessagingListener(final MessagingListener listener) {
		messagingListeners.add(listener);
	}
	
	/**
	 * Removes the messaging controllers.
	 *
	 * @param controller the controller
	 * @return true, if successful
	 */
	public boolean removeMessagingControllers(final MessagingController controller) {
		return messagingListeners.remove(controller);
	}
	
	@Override
	public void searchUsersComplete(Response result) {
		for (UserGroupListener listener : userGroupListeners) {
			listener.searchUsersComplete(result);
		}
	}

	@Override
	public void searchUsersError(ApplicationException e) {
		for (UserGroupListener listener : userGroupListeners) {
			listener.searchUsersError(e);
		}
	}

	@Override
	public void contactsModification(Contact[] contacts) {
		for (ContactlistListener listener : contactlistListeners) {
			listener.contactsModification(contacts);
		}
	}

	@Override
	public void contactModification(Contact contact, OperationType type) {
		for (ContactlistListener listener : contactlistListeners) {
			listener.contactModification(contact, type);
		}
	}

	@Override
	public void contactModification(Contact contact, String message, Type type) {
		for (ContactlistListener listener : contactlistListeners) {
			listener.contactModification(contact, message, type);
		}
	}

	@Override
	public void chatRoomsModification(ChatRoom[] chatRooms) {
		for (MessagingListener listener : messagingListeners) {
			listener.chatRoomsModification(chatRooms);
		}
	}

	@Override
	public void chatRoomModification(ChatRoom chatRoom, OperationType type) {
		for (MessagingListener listener : messagingListeners) {
			listener.chatRoomModification(chatRoom, type);
		}
	}

	@Override
	public void messageReceived(ChatRoom chatRoom, ChatMessage message) {
		for (MessagingListener listener : messagingListeners) {
			listener.messageReceived(chatRoom, message);
		}
	}

	@Override
	public void messageSend(ChatRoom chatRoom, ChatMessage message) {
		for (MessagingListener listener : messagingListeners) {
			listener.messageSend(chatRoom, message);
		}
	}

	@Override
	public void messageDelivered(ChatRoom chatRoom, ChatMessage message) {
		for (MessagingListener listener  : messagingListeners) {
			listener.messageDelivered(chatRoom, message);
		}
	}

	@Override
	public void fileReceived(ChatRoom chatRoom, FileMessage file) {
		for (MessagingListener listener : messagingListeners) {
			listener.fileReceived(chatRoom, file);
		}
	}

	@Override
	public void fileSend(ChatRoom chatRoom, FileMessage file) {
		for (MessagingListener listener : messagingListeners) {
			listener.fileSend(chatRoom, file);
		}
	}

	@Override
	public void fileDelivered(ChatRoom chatRoom, FileMessage file) {
		for (MessagingListener listener : messagingListeners) {
			listener.fileDelivered(chatRoom, file);
		}
	}

	@Override
	public void fileProgress(ChatRoom chatRoom, FileMessage file, int progress) {
		for (MessagingListener listener : messagingListeners) {
			listener.fileProgress(chatRoom, file, progress);
		}
	}

	@Override
	public void reportException( Exception e ) {
		// TODO
	}

}
